//
//  LEInputAccessoryView.m
//  CreditAddressBook
//
//  Created by LE on 15/12/17.
//  Copyright © 2015年 Lee. All rights reserved.
//

#import "LEInputAccessoryView.h"

@interface LEInputAccessoryView ()

@property(nonatomic, strong) NSArray *textFields;
@property(nonatomic, strong) UIView *showView;
@end

@implementation LEInputAccessoryView

- (instancetype)initWithTextFields:(NSArray *)textFields view:(UIView *)view {
    if (self = [super init]) {
        self.textFields = textFields;
        self.showView = view;
        [self createInputAccessoryView];
    }
    return self;
}

- (void)createInputAccessoryView {
    UIBarButtonItem *prevButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:101 target:nil action:@selector(gotoPrevTextfield)];
    UIBarButtonItem *nextButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:102 target:nil action:@selector(gotoNextTextfield)];
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:nil action:@selector(dismissKeyboard)];
    UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    UIBarButtonItem *placeholder = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
    
    [self sizeToFit];
    [self setItems:[NSArray arrayWithObjects:prevButton, placeholder, nextButton, placeholder, flexSpace, placeholder, doneButton, nil] animated:YES];
}

- (void)gotoPrevTextfield {
    NSUInteger index = [self.textFields indexOfObject:self.editingTextField];
    self.editingTextField = [self.textFields objectAtIndex:(index - 1)];
    [self.editingTextField becomeFirstResponder];
}

- (void)gotoNextTextfield {
    NSUInteger index = [self.textFields indexOfObject:self.editingTextField];
    self.editingTextField = [self.textFields objectAtIndex:(index + 1)];
    [self.editingTextField becomeFirstResponder];
}

- (void)dismissKeyboard {
    [self.showView endEditing:YES];
}
@end
